package com.shunli.cterminal.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examples.core.enums.CommonStatusEnum;
import com.shunli.cterminal.entity.SystemTrafficDomainConfiguration;
import com.shunli.cterminal.mapper.PromotionConfigMapper;
import com.shunli.cterminal.mapper.SystemTrafficDomainConfigurationMapper;
import com.shunli.cterminal.mapstruct.SystemTrafficDomainConfigurationConverter;
import com.shunli.cterminal.model.bo.PromotionConfigBO;
import com.shunli.cterminal.model.res.TrafficDomainVO;
import com.shunli.cterminal.service.ISystemTrafficDomainConfigurationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 项目方系统流量域名规则配置 服务实现类
 * </p>
 *
 * @author daodao
 * @since 2025-02-10 14:54:18
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SystemTrafficDomainConfigurationServiceImpl extends ServiceImpl<SystemTrafficDomainConfigurationMapper, SystemTrafficDomainConfiguration> implements ISystemTrafficDomainConfigurationService {

    private final SystemTrafficDomainConfigurationConverter systemTrafficDomainConfigurationConverter = SystemTrafficDomainConfigurationConverter.INSTANCE;
    private final PromotionConfigMapper promotionConfigMapper;

    /**
     * 查询有效的流量域名规则配置
     * @param promotionCode 推广渠道编码
     * @return SystemTrafficDomainConfiguration
     */
    @Override
    public TrafficDomainVO queryEffectiveByPromotionCode(String promotionCode) {
        if (StrUtil.isBlank(promotionCode)) {
            return null;
        }
        LambdaQueryWrapper<SystemTrafficDomainConfiguration> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SystemTrafficDomainConfiguration::getPromotionCode, promotionCode);
        lambdaQueryWrapper.eq(SystemTrafficDomainConfiguration::getStatus, CommonStatusEnum.ENABLE.getCode());
        SystemTrafficDomainConfiguration systemTrafficDomainConfiguration = this.getOne(lambdaQueryWrapper, false);
        if (ObjectUtil.isNull(systemTrafficDomainConfiguration)) {
            return null;
        }
        // 查询全部 在分页
        List<PromotionConfigBO> result = promotionConfigMapper.queryPromotionConfigList(null, null,promotionCode);
        if (CollUtil.isEmpty(result) || ObjectUtil.isEmpty(result.get(0))) {
            return null;
        }

        TrafficDomainVO trafficDomainVO = systemTrafficDomainConfigurationConverter.entityToResVO(systemTrafficDomainConfiguration);
        PromotionConfigBO promotionSelectVo = result.get(0);
        this.paddingTrafficDomainInfo(trafficDomainVO, promotionSelectVo);
        return trafficDomainVO;
    }

    private void paddingTrafficDomainInfo(TrafficDomainVO trafficDomainVO, PromotionConfigBO bo) {
        if (ObjectUtil.isNotNull(bo)) {
            trafficDomainVO.setDomain(bo.getDomain());
            trafficDomainVO.setAccountName(bo.getAccountName());
            trafficDomainVO.setPlatform(bo.getPlatform());
            trafficDomainVO.setPlatformName(bo.getPlatformName());
            trafficDomainVO.setPromotionChannel(bo.getPromotionChannel());
            trafficDomainVO.setSkuId(bo.getSkuId());
            trafficDomainVO.setPrice(bo.getPrice());
            trafficDomainVO.setGoodsId(bo.getGoodsId());
            trafficDomainVO.setGoodsName(bo.getGoodsName());
            trafficDomainVO.setChannelGoodId(bo.getChannelGoodId());
            trafficDomainVO.setTemplateId(bo.getTemplateId());
            trafficDomainVO.setTemplateName(bo.getTemplateName());
        }
    }
}
